package com.gengzp.common.model.pagination.resp;

import com.gengzp.common.model.pagination.req.PaginationReq;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * @ClassName PaginationResponse
 * @Description 分页查询返回结果实体类, 分页数据需要作为该类的泛型返回给前端
 * @Author gengzp
 * @Date 2025/5/11 0:17
 */
@Data
@ApiModel(description = "分页查询返回结果实体类")
public class PaginationResponse<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -8618396462873024732L;

    /**
     * 当前页码
     */
    @ApiModelProperty(value = "当前页码")
    private Integer pageNum;

    /**
     * 当前查询的每页数据条数
     */
    @ApiModelProperty(value = "当前查询的每页数据条数")
    private Integer pageSize;

    /**
     * 所有数据总条数
     */
    @ApiModelProperty(value = "所有数据总条数")
    private Integer total;

    /**
     * 所有数据总页数
     */
    @ApiModelProperty(value = "所有数据总页数")
    private Integer pageCount;

    /**
     * 分页查询返回数据
     */
    @ApiModelProperty(value = "分页查询返回数据")
    private List<T> records;

    /**
     * 构造方法私有化，禁止外部直接构造对象
     */
    private PaginationResponse(Integer pageNum, Integer pageSize, Integer total, Integer pageCount, List<T> records) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.pageCount = pageCount;
        this.records = records;
    }

    /**
     * 获取分页查询结果
     *
     * @param records       分页查询的集合数据
     * @param paginationReq 分页查询请求参数
     * @return 构建好的PaginationResult实例
     */
    public static <T> PaginationResponse<T> get(List<T> records, PaginationReq paginationReq) {
        return new PaginationResponse<>(paginationReq.getPageNum(),
                paginationReq.getPageSize(),
                paginationReq.getTotal(),
                paginationReq.getPageCount(),
                records);
    }

}
